package com.zenithmind.library.pojo.vo;

import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;

import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * 作者视图对象
 * 
 * @author ZenithMind
 * @since 2024-06-14
 */
@Data
@Schema(name = "作者视图对象", description = "作者信息的视图对象")
public class AuthorVO {

    @Schema(description = "作者ID")
    private String id;

    @Schema(description = "作者姓名")
    private String name;

    @Schema(description = "作者英文名")
    private String nameEn;

    @Schema(description = "笔名")
    private String penName;

    @Schema(description = "性别")
    private Integer gender;

    @Schema(description = "性别名称")
    private String genderName;

    @Schema(description = "出生日期")
    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private LocalDate birthDate;

    @Schema(description = "逝世日期")
    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private LocalDate deathDate;

    @Schema(description = "国籍")
    private String nationality;

    @Schema(description = "作者简介")
    private String biography;

    @Schema(description = "作者头像")
    private String avatar;

    @Schema(description = "官方网站")
    private String website;

    @Schema(description = "状态")
    private Integer status;

    @Schema(description = "状态名称")
    private String statusName;

    @Schema(description = "作品数量")
    private Integer bookCount;

    @Schema(description = "总借阅次数")
    private Long totalBorrowCount;

    @Schema(description = "创建时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime createTime;

    @Schema(description = "更新时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime updateTime;

    /**
     * 获取性别名称
     */
    public String getGenderName() {
        if (gender == null) {
            return "未知";
        }
        return switch (gender) {
            case 0 -> "女";
            case 1 -> "男";
            case 2 -> "其他";
            default -> "未知";
        };
    }

    /**
     * 获取状态名称
     */
    public String getStatusName() {
        if (status == null) {
            return "未知";
        }
        return switch (status) {
            case 0 -> "禁用";
            case 1 -> "启用";
            default -> "未知";
        };
    }

    /**
     * 计算年龄
     */
    public Integer getAge() {
        if (birthDate == null) {
            return null;
        }
        LocalDate endDate = deathDate != null ? deathDate : LocalDate.now();
        return endDate.getYear() - birthDate.getYear();
    }

    /**
     * 判断是否在世
     */
    public boolean isAlive() {
        return deathDate == null;
    }
}
